var cities = require('../../citylist.js');
var config = require('../../config.js');
var amapFile = require('../../libs/amap-wx.js');
var util = require('../../network_util.js');
var promise = require('../../libs/es6-promise.min.js')
var commonjs = require('../../common.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    localCityId: 0,
    localCityName: "",
    cities: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ localEnable: "disable" });
    this.doLocation();
  },
  doLocation: function () {
    var localCityName = wx.getStorageSync("localCityName");
    if(!commonjs.isEmpty(localCityName)){
      this.setData({ localCityName: localCityName});
      this.getOpeningCity();
    }else{
    var that = this;
    var key = config.aMapKey;
    var myAmapFun = new amapFile.AMapWX({ key: key });
    myAmapFun.getRegeo({
      iconPath: "../../images/marker.png",
      iconWidth: 22,
      iconHeight: 32,
      success: function (data) {
        console.log(data);
        // console.log(data[0].regeocodeData.addressComponent.city);
        that.setData({ localCityName: data[0].regeocodeData.addressComponent.city });
        that.getOpeningCity();
      },
      fail: function (info) {
        // wx.showModal({title:info.errMsg})
      }
    })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getOpeningCity: function () {
    var openingCities = wx.getStorageSync("openingCities");
    // if (!commonjs.isEmpty(openingCities)) {
    //   this.handleCities(openingCities);
    //   // this.setData({ cities: cities });
    // } else {
      var that = this;
      util._get(config.fromCityList,
        function (res) {
          console.log("city-from.get.fromCityList.01");
          console.log(res);
          that.handleCities(res.data.data);
        }, function (res) {
          console.log("get fromCityList fail: ");
          console.log(res);
        })
    // }
  },
  handleCities: function (data) {
    var cities = new Array();
    var that = this;
    //过滤没配置航站楼的城市、没开放的城市
    data.forEach(function (item, index, input) {
      if (item.numbers > 0 && item.isOpen == 1) {
        cities.push(item);
      }
      console.log(item.cityName.replace("市", "") + " | " + that.data.localCityName.replace("市", ""));
      if (item.cityName.replace("市", "") == that.data.localCityName.replace("市", "")) {
        that.setData({ localCityId: item.id });
        if (that.data.localCityId > 0) {
          that.setData({ localEnable: "enable" }); //定位所在城市可用
        }
      }
    })

    this.setData({ cities: cities });
    // wx.setStorageSync("openingCities", cities);
    // return cities;
  },
  onCityTap: function (e) {
    if (this.data.localEnable == "disable") {
      if (e.currentTarget.dataset.text.replace("市", "") == this.data.localCityName.replace("市", "")) {
        wx.showToast({
          title: '该城市尚未开通',
        })
        return;
      }
    }
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面 
    var prevPage = pages[pages.length - 2]; //上一个页面 
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去 
    console.log(e.currentTarget);
    var fromInfo = app.globalData.fromInfo;
    fromInfo.cityId = e.currentTarget.dataset.cityid;
    fromInfo.cityName = e.currentTarget.dataset.text;

    app.globalData.fromInfo = fromInfo;
    prevPage.setData({ fromInfo: fromInfo });
    prevPage.getTerminalList(fromInfo.cityId);
    wx.navigateBack({

    })
  }
})